<?php
session_start();
header("Content-Type: text/html; charset=utf-8");

$comprobando = false;
$sending = false;
$sended = false;
$error_sended = true;
$error_duplicate = false;
$error_url = false;
$error_tweet = false;

if (isset($_REQUEST['send']) && $_REQUEST['send'] == "true") {
    $sending = true;
    $user = $_REQUEST['user'];
    $tweet = urlencode($_REQUEST['tweet']);
    $date_created = $_REQUEST['date_created'];
    $tweet_id = $_REQUEST['tweet_id'];

    $result = "http://nibble.li/twittertido/web/insert.php?user=anonymous&user_tweet=$user&tweet_url=$tweet_id&tweet=$tweet&date_created=$date_created";
    $correct = file_get_contents($result, 0, null, null);
    if ($correct > 0) {
        $error_sended = false;
        $sended = true;
    } else if ($correct == -2) {
        $error_sended = false;
        $error_duplicate = true;
    }
}

if (isset($_REQUEST['tweet_url']) && $_REQUEST['tweet_url'] != "") {
    $comprobando = true;
    $url = $_POST['tweet_url'];
    $pos_twitter = strpos($url, "twitter");
    $pos_status = strpos($url, "status");


    if ($pos_status === false || $pos_twitter === false) {
        $error_url = true;
    } else {
        $pos_slash = strrpos($url, '/', -1) + 1;
        $tweet_id = substr($url, $pos_slash, 18);
        $jsonurl = "https://api.twitter.com/1/statuses/show/" . $tweet_id . ".json";
        $json = file_get_contents($jsonurl, 0, null, null);
        $tweet = json_decode($json, true);

        if (strlen($tweet['text']) <= 0) {
            $error_tweet = true;
        }
    }
}
?>
<!-DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Twittertido - Procastination everywhere!!!</title>
        <link rel="stylesheet" href="css/glamour.css" />
        <script src="js/jquery-1.6.2.min.js"></script>
        <script src="js/common.js"></script>
        <style>
            input{
                width: 80%;
            }
        </style>
    </head>
    <body>

        <?php include_once("header.php"); ?>

        <div class="wrapper">
            <div class="content">
                <?php if ($sended) { ?><div class="ok mt_five">El tweet se ha enviado corrextamente. Muchas gracias!<br /></div><?php } ?>
                <form method="post" id="form_send" >
                    <div class="mt_five mb_five">Introduzca la URL del tweet <br /></div>
                    <?php if ($comprobando && $error_tweet) { ?><div class="error mt_five">No se puede obtener el Tweet. Compruebe la url, por favor</div><?php } ?>
                    <?php if ($sending && $error_sended) { ?><div class="error mt_five" >Ha ocurrido un error al enviar el tweet. Lo sentimos mucho</div><?php } ?>
                    <?php if ($sending && $error_duplicate) { ?><div class="error mt_five">El tweet que estás enviando ya se encuentra en la cola. Gracias!</div><?php } ?>
                    <div class="clear" ></div>
                    <input type="text" id="tweet_url" name="tweet_url" placeholder="https://twitter.com/#!/juanpatarino/status/162475009468669952"
                           <?php if ($comprobando) { ?>value="<?php print $url; ?>" <?php } ?>/>
                    <div class="clear" ><br /></div>
                    <div onclick="$('#form_send').submit()" class="button">Comprobar</div>
                </form>

                <?php if ($comprobando && !$error_tweet) { ?>
                    <div class="tweet">
                        <form id="form_send_tweet" method="post">
                            <input type="hidden" id="send" name="send" value="true" />
                            <input type="hidden" id="user" name="user" value="<?php print $tweet['user']['screen_name']; ?>" />
                            <input type="hidden" id="tweet" name="tweet" value="<?php print $tweet['text']; ?>" />
                            <input type="hidden" id="date_created" name="date_created" value="<?php print $tweet['date_created']; ?>" />
                            <input type="hidden" id="tweet_id" name="tweet_id" value="<?php print $tweet_id; ?>" />

                            <table>
                                <tr>
                                    <td class="avatar">
                                        <img src="https://api.twitter.com/1/users/profile_image?screen_name=<?php print $tweet['user']['screen_name']; ?>&size=normal" />
                                    </td>
                                    <td>
                                        <div class="title"><?php print $tweet['user']['screen_name']; ?></div>
                                        <div><?php print $tweet['text']; ?></div>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2" class="tar">
                                        <div class="button fr" onclick="$('#form_send_tweet').submit()" >Enviar</div>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                <?php } ?>
            </div>
            <br />
            <!--div class="content" style="padding: 20px;"-->
            <a href="https://chrome.google.com/webstore/detail/igdldalhphpcbeedbdekfiopnapbngeg" target="_blank"><img src="img/banner_chrome.png" alt="Twittertido para Chrome" /></a><br /><br />
                <a href="https://market.android.com/details?id=li.nibble.twittertido" target="_blank"><img src="img/banner_android.png" alt="Twittertido para Android" /></a>
            <!--/div-->
        </div>
    </body>
</html>